package server

import (
	"enjoy_wangyashuo/formdata"
	"enjoy_wangyashuo/internal/model/good"
	"enjoy_wangyashuo/pkg"
	"errors"
	"fmt"
	"gorm.io/gorm"
	"strconv"
)

// 商品详情
func GoodsDetail(data formdata.GoodsDetail) (goodsInfo formdata.GoodsInfo, err error) {
	//查询商品是否存在
	goodsId, _ := strconv.Atoi(data.GoodsId)
	g := good.Goods{
		Model: gorm.Model{ID: uint(goodsId)},
	}
	detail, err := g.GoodsDetailById()
	if err != nil {
		return formdata.GoodsInfo{}, err
	}
	var goodsID []int64
	goodsID = append(goodsID, int64(detail.ID))
	pg := new(good.GoodsProperty)
	//根据商品id查询属性值信息  gidVal（包含属性和属性值）
	infoList, err := pg.GetGoodsPropertyValByGid(goodsID)
	if err != nil {
		return formdata.GoodsInfo{}, err
	}

	// 创建一个 map 来存储属性及其对应的属性值
	info := make(map[int64]formdata.PropertyVal)

	// 初始化属性信息
	for _, val := range infoList.Property {
		info[int64(val.ID)] = formdata.PropertyVal{
			PrId:        int64(val.ID),
			Name:        val.Name,
			PropertyVal: []formdata.GVals{},
		}
	}
	// 将属性值放入对应的属性中
	Images := pkg.GetImages(detail.Img) // 解析json格式图片
	for _, val := range infoList.PropertyVal {
		if prop, ok := info[val.PropertyID]; ok { //如果找到啦这个id就储存对应的数据
			prop.PropertyVal = append(prop.PropertyVal, formdata.GVals{
				ValId: int64(val.ID),
				PrId:  val.PropertyID,
				Name:  val.Name,
				Img:   Images,
			})
			info[val.PropertyID] = prop
		}
	}

	// 将 map 转换为 slice
	var properties []formdata.PropertyVal
	for _, val := range info {
		properties = append(properties, val)
	}
	goodsInfo = formdata.GoodsInfo{
		GoodsID:     int64(detail.ID),
		Title:       detail.Title,
		Point:       detail.Point,
		Image:       Images,
		Description: detail.Description,
		Property:    properties, // 这里返回多个属性
		TypesID:     detail.TypesID,
	}
	return goodsInfo, nil
}

// 根据skuID 查询商品信息
func GetGoodsSkuInfoBySkuID(data formdata.GetGoodsSkuInfoReq) ([]formdata.GoodsInfo, error) {
	var Skuid []int64
	id, err := strconv.Atoi(data.SkuId)
	Skuid = append(Skuid, int64(id))
	u := new(good.GoodSku)
	skuInfo, err := u.GetGoodsSkuInfoBySkuID(Skuid)
	if err != nil {
		return nil, err
	}
	fmt.Println("sku信息", data.SkuId, skuInfo)
	var ids []int64     //储存多个商品id
	var images []string //储存图片
	for _, val := range skuInfo {
		ids = append(ids, val.GoodsID)
		images = append(images, val.Img)
	}
	//查询商品表 多个商品
	goo := new(good.Goods)
	goodsInfoList, err := goo.GoodsDetailByIds(ids)
	if err != nil {
		return nil, err
	}
	fmt.Println("商品信息", goodsInfoList)

	//查询属性值
	gv := new(good.GoodsProperty)
	//根据商品id查询属性值信息  （infoList包含属性 和属性值）
	infoList, err := gv.GetGoodsPropertyValByGid(ids)
	if err != nil {
		return nil, err
	}

	// 创建一个 map 来存储属性及其对应的属性值
	info := make(map[int64]formdata.PropertyVal, len(infoList.Property))
	// 初始化属性信息
	for _, val := range infoList.Property {
		info[int64(val.ID)] = formdata.PropertyVal{
			PrId:        int64(val.ID),
			Name:        val.Name,
			PropertyVal: []formdata.GVals{},
		}
	}
	// 将属性值放入对应的属性中

	for _, val := range infoList.PropertyVal {
		if prop, ok := info[val.PropertyID]; ok { //如果找到啦这个id就储存对应的数据
			prop.PropertyVal = append(prop.PropertyVal, formdata.GVals{
				ValId: int64(val.ID),
				PrId:  val.PropertyID,
				Name:  val.Name,
			})
			info[val.PropertyID] = prop
		}
	}

	// 将 map 转换为 slice
	var properties []formdata.PropertyVal
	for _, val := range info {
		properties = append(properties, val)
	}

	var goodsInfos []formdata.GoodsInfo
	for _, val := range goodsInfoList {
		Images := pkg.GetImages(images[0]) // 解析json格式图片
		goodsInfos = append(goodsInfos, formdata.GoodsInfo{
			GoodsID:     int64(val.ID),
			Title:       val.Title,
			Point:       val.Point,
			Image:       Images,
			Description: val.Description,
			Property:    properties, // 这里返回多个属性
			TypesID:     val.TypesID,
		})
	}
	return goodsInfos, nil
}

// 查询sku信息
func GetSkuInfo(data formdata.GoodsDetail) (goodsInfo formdata.GoodsSkuInfo, err error) {
	//查询商品是否存在
	goodsID, _ := strconv.Atoi(data.GoodsId)
	g := good.Goods{
		Model: gorm.Model{ID: uint(goodsID)},
	}
	detail, err := g.GoodsDetailById()
	if err != nil {
		return formdata.GoodsSkuInfo{}, err
	}
	Images := pkg.GetImages(detail.Img) // 解析json格式图片
	//查询sku信息 如果没有值默认查询最小的数据
	sk := good.GoodSku{
		ProValueID1: data.V1,
		ProValueID2: data.V2,
		ProValueID3: data.V3,
		GoodsID:     int64(detail.ID),
	}
	return sk.GetGoodsDetailByVal(Images)
}

// 商品搜索列表
func SearchGoodsList(data formdata.GoodsSearch) ([]formdata.GoodsInfoList, error) {
	g := good.Goods{
		Title:   data.Title,
		TypesID: data.TypesID,
	}
	searchGoodsInfo, err := g.SearchGoodsList(data.Page, data.PageSize)
	if err != nil {
		return nil, errors.New("没有这个商品")
	}
	return searchGoodsInfo, nil
}

// 商品分类列表
func CategoryList() ([]good.CategoryLists, error) {
	c := good.GoodsCategory{}
	list, err := c.GetCategoryList()
	if err != nil {
		return nil, errors.New("列表查询失败")
	}
	return list, nil
}

// 分类位置列表
func CategoryLocationList(data formdata.GoodsCategoryLocation) ([]good.CategoryLists, error) {
	c := good.GoodsCategory{
		Location: data.Location,
	}
	fmt.Println("1111", data.Location)
	list, err := c.CategoryList()
	if err != nil {
		return nil, errors.New("列表查询失败")
	}
	return list, nil
}

// 分类id查询商品
func CategoryIdByGoodsInfo(data formdata.CategoryByIDGoodsInfo) ([]formdata.GoodsInfoList, error) {
	c := good.Goods{
		TypesID: data.CategoryID,
	}
	return c.GoodsInfoByCategoryId()
}
